package com.tomowork.service.impl;

import com.tomowork.dao.IUserDAO;
import com.tomowork.entity.User;
import com.tomowork.entity.dto.UserDto;
import com.tomowork.service.IUserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDAO iUserDAO ;
    @Override
    public User selectByUsernameAndPwd(String username) {
        return iUserDAO.selectByUsernameAndPwd(username);
    }

    @Override
    @Transactional
    public boolean save(User user) {
        //todo id需要改进
        user.setId(UUID.randomUUID().toString().replaceAll("-","").substring(0,6));
        user.setNickname(user.getUsername());
        user.setRegistTime(new Date());
        user.setIsDelete(1);
        String saltPassword = new Md5Hash(user.getPassword(),user.getUsername(),10).toString() ;
        user.setPassword(saltPassword);
        return iUserDAO.insert(user) ;
    }

    @Override
    public boolean checkExist(String username) {
        return iUserDAO.selectByUsername(username);
    }


}
